package com.itfitness.mchart.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.itfitness.mchart.R;
import com.itfitness.mchart.utils.MTextUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: MChart
 * @Package: com.itfitness.mchart.activity
 * @ClassName: LoginActivity
 * @Description: java类作用描述 ：登录
 * @Author: 作者名：lml
 * @CreateDate: 2019/4/24 10:23
 * @UpdateUser: 更新者：
 * @UpdateDate: 2019/4/24 10:23
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.activity_login_img_logo)
    ImageView activityLoginImgLogo;
    @BindView(R.id.activity_login_et_username)
    EditText activityLoginEtUsername;
    @BindView(R.id.activity_login_et_password)
    EditText activityLoginEtPassword;
    @BindView(R.id.activity_login_bt_login)
    Button activityLoginBtLogin;
    @BindView(R.id.activity_login_tv_regist)
    TextView activityLoginTvRegist;

    @Override
    protected int LayoutRes() {
        return R.layout.activity_login;
    }

    @Override
    protected boolean isHaveTitle() {
        return false;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        BarUtils.setStatusBarAlpha(this,255);
        ButterKnife.bind(this);
        EMClient.getInstance().logout(false);

    }

    @OnClick({R.id.activity_login_bt_login, R.id.activity_login_tv_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_login_bt_login:
                login();
                break;
            case R.id.activity_login_tv_regist:
                gotoActivity(RegistActivity.class);
                break;
        }
    }

    private void login() {
        String username = MTextUtil.getText(activityLoginEtUsername);
        String password = MTextUtil.getText(activityLoginEtPassword);
        EMClient.getInstance().login(username,password,new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
                gotoActivity(MainActivity.class);
                finish();
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.showShort(message);
            }
        });

    }
}
